<template>
	<view class="login-container">
		<view class="form-container">
			<h1 class="login-title">登录</h1>
			<input v-model="account" placeholder="请输入账号" v-if="isAccountLogin" class="input-field1" />
			<input v-model="phone" placeholder="请输入手机号码" v-else class="input-field1" />
			<input v-model="password" type="password" placeholder="请输入密码" v-if="isAccountLogin" class="input-field1" />
			<view v-else class="code-input-container">
				<input v-model="code" placeholder="请输入验证码" class="input-field" />
				<button @click="sendCode" class="get-code-button">获取验证码</button>
			</view>
			<button @click="login" class="login-button">登录</button>
			<view class="toggle-login-mode" @click="toggleLoginMode">
				{{ isAccountLogin ? '手机验证码登录' : '账号密码登录' }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isAccountLogin: true,
				account: '',
				phone: '',
				password: '',
				code: ''
			}
		},
		methods: {
			login() {
				if (this.isAccountLogin) {
					// 账号密码登录逻辑
					console.log('账号:', this.account, '密码:', this.password);
					uni.request({
						url: 'http://localhost:8006/user/login', // 服务器url地址
						method: 'POST',
						header: {
							'Content-Type': 'application/json' // 确保设置正确的Content-Type
						},
						data: { // 直接传递对象，不需要手动转换为JSON字符串
							userAccount: this.account,
							userPassword: this.password,
						},
						success: (res) => {
							// 请求成功的处理逻辑
							console.log(res.data.data, 48);
							this.$store.commit('setUserInfo', res.data.data);
							uni.navigateTo({
								url: '/pages/index/index'
							});
						},
						fail: (err) => {
							// 请求失败的处理逻辑
							console.error('请求失败:', err);
						},
						complete: () => {
							// 请求完成的处理逻辑（无论成功与否）
							console.log('请求完成');
						}
					});


				} else {
					// 手机号码+验证码登录逻辑
					console.log('手机号码:', this.phone, '验证码:', this.code);
				}
			},
			toggleLoginMode() {
				this.isAccountLogin = !this.isAccountLogin;
				this.account = '';
				this.phone = '';
				this.password = '';
				this.code = '';
			},
			sendCode() {
				// 发送验证码逻辑
				console.log('发送验证码到手机号:', this.phone);
			}
		}
	}
</script>

<style>
	.login-container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
		background-color: #f7f7f7;
	}

	.form-container {
		width: 90%;
		max-width: 320px;
		padding: 40px 20px;
		background-color: #ffffff;
		border-radius: 8px;
		box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
		box-sizing: border-box;
		/* 确保padding不会影响容器宽度 */
	}

	.login-title {
		font-size: 28px;
		font-weight: 600;
		color: #333333;
		margin-bottom: 30px;
		text-align: center;
		/* 添加这一行以使标题文本居中 */
	}
	.input-field1{
		width: auto;
		/* 缩短输入框的宽度 */
		padding: 12px 15px;
		margin-bottom: 20px;
		border: 1px solid #d9d9d9;
		border-radius: 4px;
		font-size: 16px;
	}
	.input-field {
		width: 100%;
		padding: 12px 15px;
		margin-bottom: 20px;
		border: 1px solid #d9d9d9;
		border-radius: 4px;
		font-size: 16px;
	}

	.login-button {
		width: 100%;
		padding: 12px 0;
		background-color: #409eff;
		color: #ffffff;
		border: none;
		border-radius: 4px;
		font-size: 18px;
		cursor: pointer;
		transition: background-color 0.3s;
		/* 添加过渡效果 */
	}

	.login-button:hover {
		background-color: #3a8ee6;
	}

	.toggle-login-mode {
		margin-top: 20px;
		text-align: center;
		color: #409eff;
		font-size: 14px;
		cursor: pointer;
	}

	.code-input-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 100%;
	}

	.input-field {
		width: 40%;
		/* 缩短输入框的宽度 */
		padding: 12px 15px;
		margin-bottom: 20px;
		border: 1px solid #d9d9d9;
		border-radius: 4px;
		font-size: 16px;
	}

	.get-code-button {
		flex-grow: 1;
		/* 允许按钮扩展以填充剩余空间 */
		/* padding: 10px 15px; */
		padding-top: 5px;
		background-color: #409eff;
		color: #ffffff;
		border: none;
		border-radius: 4px;
		font-size: 16px;
		cursor: pointer;
		transition: background-color 0.3s;
		margin-left: 10px;
		margin-top: -3vh;
		height: 7vh;
		/* 添加左边距 */
	}

	.get-code-button:hover {
		background-color: #3a8ee6;
	}
</style>